/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       directionalblur.cpp
 * Author:     twoja stara
 * Created on: 2010-05-30
 *
 **********************************************************************************************************************/

#include "displaylist.h"
#include "directionalblur.h"

namespace mGameEngine
{
namespace Graphics
{

DirectionalBlur::DirectionalBlur(float angle, float size,
                                const Texture *input, RenderTarget *output) :
    PostProcess(input, output)
{
    setDirectionAngle(angle);
    setSize(size);
}

DirectionalBlur::~DirectionalBlur()
{
}

void DirectionalBlur::process() const
{
    // create and compile GPU program
    static GPUProgram gpuProgram(
#include "postprocess.vert"
        ,
#include "directionalblur.frag"
        , true);

    // no input or output
    if(!_input || !_output)
    {
        return;
    }

    // post-process is disabled
    if(!_enabled || !_size)
    {
        return _passUnprocessed();
    }

    // use program
    gpuProgram.use();
    gpuProgram.setTexture("texture", _input, 0);

    Vector2 direction(cos(_angle), sin(_angle));
    direction *= _size;
    direction /= _output->getSize();
    gpuProgram.setVector("direction", direction);

    // render image
    _output->beginRendering();

    DISPLAY_LIST
    (
        glDisable(GL_DEPTH_TEST);
        glBegin(GL_TRIANGLE_STRIP);
            glVertex2fv(Vector2(0.0f, 0.0f));
            glVertex2fv(Vector2(1.0f, 0.0f));
            glVertex2fv(Vector2(0.0f, 1.0f));
            glVertex2fv(Vector2(1.0f, 1.0f));
        glEnd();
    );

    _output->endRendering();
}

}
}

